//
//  SYKaoShiTongJiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/26.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYKaoShiTongJiView.h"
#import "BanJiTopView.h"
#import "SYTongJiTopView.h"
#import "SYTongJiNameView.h"
#import "SYTopLabView.h"
#import "SYTongJiTypeView.h"
#import "SYTongJiKaoshiTableViewCell.h"
#import "SYKSShiTiDetailsView.h"//试题详情
#import "SYKaoShiJuanDetailsView.h"
#import "SYKaoShiBanJiView.h"
#import "SYKSXuanZeXueShengView.h"
#import "SYTJStudentsTableViewCell.h"
#import "SYKaoShiTopView.h"

#import "SYLianXiTopView.h"
#import "SYLXStudentsTableViewCell.h"
#import "SYTongJiLianXiTableViewCell.h"
#import "SYKeWenTableViewCell.h"
#import "WMPhotoBrowser.h"
#import "SYRequestAPI+YueJuan.h"


@interface SYKaoShiTongJiView () <BanJiTopViewSelectedDelegate,UITableViewDelegate,UITableViewDataSource,SYTongJiNameViewDelegate>{
    NSString *sort;
    WMPhotoBrowser *browser;
    UIButton *leftBtn;
} 
@property (nonatomic, strong) BanJiTopView *banJiTopView;
@property (nonatomic, strong) SYBackTopView *backTopView;
@property (nonatomic, strong) UIButton *chakanBtn;//查看试卷
@property (nonatomic, strong) SYTongJiTopView *tongjiView;//顶部
@property (nonatomic, strong) SYTongJiNameView *tjNameView;//名字


@property (nonatomic, strong) SYTopLabView *topLabView;//按试题统计
@property (nonatomic, strong) SYTopLabView *topKewenView;//考试统计 -- 按照课文统计

@property (nonatomic, strong) SYKaoShiTopView *twoTopLabView;//按学生统计
@property (nonatomic, strong) SYLianXiTopView *LXTopLabView;//练习册按学生统计

@property (nonatomic, strong) SYTongJiTypeView *tjType;//状态
@property (nonatomic, strong) UITableView *curTableView;
@property (nonatomic, assign) BOOL isSelectYJ;//
@property (nonatomic, strong) SYKaoShiBanJiView *banjiView;
@property (nonatomic, strong) SYKSXuanZeXueShengView *xueshengView;
@property (nonatomic, strong) SYExamInfoModel *infoModel;//考试信息
@property (nonatomic, strong) SYExamInfoClassesModel *classesModel;//班级信息

@property (nonatomic, copy) NSString *banjiId;//班级id
@property (nonatomic, copy) NSString *xueshengIds;//xueshengid
@property (nonatomic, copy) NSString *templateId;//模板id
@property (nonatomic, assign) BOOL isKaoShi;//yes 考试统计  no 订正统计

@property (nonatomic, assign) BOOL isTongJi;//统计格式  yes 按照试题统计  no 按照学生统计

@property (nonatomic, strong) NSMutableArray *dataListArr;//数据源

@property (nonatomic, assign) BOOL isKewen;///按照课文统计 yes
//@property (nonatomic, strong) nsmutable

@end


@implementation SYKaoShiTongJiView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
        [self initSubView];
    }
    return self;
}


- (void)initSubView {
    sort = @"0";
    self.isTongJi = YES;
    self.isKaoShi = YES;
    self.dataListArr = [NSMutableArray array];
    [self addSubview:self.backTopView];
    [self.backTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(44);
    }];
    [self addSubview:self.banJiTopView];
//    self.banJiTopView.backgroundColor = [UIColor redColor];
    [self.banJiTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backTopView.mas_left).offset(150);
        make.right.equalTo(self.backTopView.mas_right).offset(-150);
        make.top.bottom.equalTo(self.backTopView);
    }];
    [self addSubview:self.chakanBtn];
    [self.chakanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.backTopView.mas_centerY);
        make.right.equalTo(self.backTopView.mas_right).offset(-50);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(40);
    }];
    [self addSubview:self.tongjiView];
    [self.tongjiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backTopView.mas_bottom);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(50);
    }];
    
    [self addSubview:self.tjNameView];
    [self.tjNameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tongjiView.mas_bottom).offset(10);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(50);
    }];
    
    
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tjNameView.mas_bottom);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(40);
    }];
    //考试统计 --- 课文统计
    [self addSubview:self.topKewenView];
    [self.topKewenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tjNameView.mas_bottom);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(40);
    }];
    
    self.twoTopLabView = [[NSBundle mainBundle] loadNibNamed:@"SYKaoShiTopView" owner:nil options:nil].firstObject;
    self.LXTopLabView = [[NSBundle mainBundle] loadNibNamed:@"SYLianXiTopView" owner:nil options:nil].firstObject;

        __weak typeof(self) weakSelf = self;
        self.twoTopLabView.block = ^(NSInteger index) {
            if (index == 1) {//倒序
                [weakSelf daoxupailieDataListArr];
            } else {//倒序
                [weakSelf daoxupailieDataListArr];
            }
        };
        
        [self addSubview:self.twoTopLabView];
        [self.twoTopLabView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.tjNameView.mas_bottom);
            make.left.equalTo(self);
            make.right.equalTo(self.mas_right).offset(-14);
            make.height.mas_equalTo(40);
        }];
        
    
    [self addSubview:self.tjType];
    [self.tjType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-14);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
        make.height.mas_equalTo(40);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabView.mas_bottom);
        make.bottom.equalTo(self.tjType.mas_top).offset(-14);
        make.left.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-14);
    }];
    
}
//课前测展示
- (void)setType:(NSString *)type {
    _type = type;
    if ([type isEqualToString:@"1"]) {
        self.tjNameView.kewenBtn.hidden = NO;
    } else {
        self.tjNameView.kewenBtn.hidden = YES;
    }
}

- (void)daoxupailieDataListArr {
    for (NSInteger i = 0; i < self.dataListArr.count/2; i++) {
        [_dataListArr exchangeObjectAtIndex:i withObjectAtIndex:_dataListArr.count-1-i];
    }
    [self.curTableView reloadData];
}


#pragma mark - tabl
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    @try {
        if ([self.type integerValue] == 5) { //订正统计
            
            if (self.isTongJi) {// 按照试题统计
                SYLXStudentsTableViewCell *tjcell = [tableView dequeueReusableCellWithIdentifier:@"SYLXStudentsTableViewCell" forIndexPath:indexPath];
                tjcell.type = self.type;
                tjcell.questionModel = self.dataListArr[indexPath.row];
                tjcell.selectionStyle = UITableViewCellSelectionStyleNone;
                return tjcell;
            } else { //按照学生统计
                SYTongJiLianXiTableViewCell *tjcell = [tableView dequeueReusableCellWithIdentifier:@"SYTongJiLianXiTableViewCell" forIndexPath:indexPath];
                SYStatisticsByStudent *studentModel = self.dataListArr[indexPath.row];
                
                tjcell.xuhaoLab.text = SJStringWithFormat(indexPath.row+1);
                tjcell.xingmingLab.text = studentModel.studentName;
                tjcell.pigaiLab.text = [NSString stringWithFormat:@"%@/%@",studentModel.questionNum,studentModel.correctedNum];
                tjcell.duicuoLab.text = [NSString stringWithFormat:@"%@/%@",studentModel.rightNum,studentModel.wrongNum];
                tjcell.zhengqueluLab.text = [NSString stringWithFormat:@"%@%%",studentModel.rightRate];
                tjcell.defenLab.text = [NSString stringWithFormat:@"%@%%",studentModel.scoreRate];
                tjcell.fenshuLab.text = studentModel.sort;
                
                if ([studentModel.studentType isEqualToString:@"缺考"]) {
                    tjcell.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
                } else if ([studentModel.studentType isEqualToString:@"串班"]) {
                    tjcell.backgroundColor = [UIColor colorWithHexString:@"#20B097"];
                } else if ([studentModel.studentType isEqualToString:@"试听"]) {
                    tjcell.backgroundColor = [UIColor colorWithHexString:@"#FF4800"];
                } else {
                    tjcell.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
                }
                tjcell.selectionStyle = UITableViewCellSelectionStyleNone;
                return tjcell;
            }
            
        }else{
            if (self.isKewen) {
                SYKeWenTableViewCell *kewenCell = [tableView dequeueReusableCellWithIdentifier:@"SYKeWenTableViewCell" forIndexPath:indexPath];
                NSDictionary *dict = self.dataListArr[indexPath.row];
                kewenCell.xuehaoLab.text = SJStringWithFormat(indexPath.row);
                kewenCell.xingmingLab.text = [dict nilObjectForKey:@"studentName"];
                kewenCell.xuehaoLab.text = [dict nilObjectForKey:@"serial"];
                kewenCell.zongfenLab.text = [dict nilObjectForKey:@"totalScore"];
                kewenCell.defenLab.text = [dict nilObjectForKey:@"score"];
                kewenCell.defenluLab.text = [dict nilObjectForKey:@"scoreRate"];
                kewenCell.zongrenshuLab.text = [dict nilObjectForKey:@"totalStuNum"];
                kewenCell.paimingLab.text = [dict nilObjectForKey:@"ranking"];
                return kewenCell;
            } else {
                if (self.isTongJi) {// 按照试题统计
                    SYTJStudentsTableViewCell *tjcell = [tableView dequeueReusableCellWithIdentifier:@"SYTJStudentsTableViewCell" forIndexPath:indexPath];
                    tjcell.type = self.type;
                    tjcell.questionModel = self.dataListArr[indexPath.row];
                    tjcell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return tjcell;
                } else { //按照学生统计
                    SYTongJiKaoshiTableViewCell *tjcell = [tableView dequeueReusableCellWithIdentifier:@"SYTongJiKaoshiTableViewCell" forIndexPath:indexPath];
                    SYStatisticsByStudent *studentModel = self.dataListArr[indexPath.row];
                    
                    tjcell.xuhaoLab.text = SJStringWithFormat(indexPath.row+1);
                    tjcell.xingmingLab.text = studentModel.studentName;
                    tjcell.pigaiLab.text = [NSString stringWithFormat:@"%@/%@",studentModel.questionNum,studentModel.correctedNum];
                    tjcell.duicuoLab.text = [NSString stringWithFormat:@"%@/%@",studentModel.rightNum,studentModel.wrongNum];
                    tjcell.zhengqueluLab.text = [NSString stringWithFormat:@"%@%%",studentModel.rightRate];
                    tjcell.defenLab.text = [NSString stringWithFormat:@"%@%%",studentModel.scoreRate];
                    if ([self.type integerValue] == 5) {
                        tjcell.fenshuLab.text = @"";
                        
                    }else{
                        tjcell.fenshuLab.text = studentModel.score;
                        
                    }
                    tjcell.paimingLab.text = studentModel.sort;
                    
                    if ([studentModel.studentType isEqualToString:@"缺考"]) {
                        tjcell.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
                    } else if ([studentModel.studentType isEqualToString:@"串班"]) {
                        tjcell.backgroundColor = [UIColor colorWithHexString:@"#20B097"];
                    } else if ([studentModel.studentType isEqualToString:@"试听"]) {
                        tjcell.backgroundColor = [UIColor colorWithHexString:@"#FF4800"];
                    } else {
                        tjcell.backgroundColor = [UIColor colorWithHexString:@"#FFFFFF"];
                    }
                    tjcell.selectionStyle = UITableViewCellSelectionStyleNone;
                    return tjcell;
                }
            }
            
        }

    } @catch (NSException *exception) {
        
    } @finally {
        
    }
   
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (self.isTongJi== YES) {//按照试题统计 跳转到试题详情
        
        SYStatisticsByQuestion *questionModel = self.dataListArr[indexPath.row];
        
        SYKSShiTiDetailsView *shidetailsView = [[SYKSShiTiDetailsView alloc] initWithFrame:CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT)];
        
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        [window addSubview:shidetailsView];
        [UIView animateWithDuration:kAnimate animations:^{
            shidetailsView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
        } completion:^(BOOL finished) {
            [shidetailsView requestDataTemplateId:questionModel.templateQuestionId studentsIds:self.xueshengIds isTongJiType:self.isKaoShi];
        }];
        
        
    } else {//按照学生统计 跳转到做卷详情
        SYStatisticsByStudent *studentModel = self.dataListArr[indexPath.row];
        
        if ([studentModel.studentType isEqualToString:@"缺考"]) {
            [XSInfoView showInfo:@"该学生缺考了暂无试卷详情" onSubView:self];
            return;
        }
        
         [MBProgressHUD mj_showMessage:nil toView:self];
        [SYRequestAPI yj_requeststatisticsAnsweredPaperDetailStudentId:studentModel.studentId templateId:self.templateId kaoshiOrDingzheng:self.isKaoShi success:^(NSMutableArray *papeFilePath) {
            [MBProgressHUD mj_hideHUDForView:self];
            
            if (papeFilePath.count == 0) {
                [XSInfoView showInfo:@"暂无试卷详情~！" onSubView:self];
                return ;
            }
//            SYKaoShiJuanDetailsView  *shidetailsView = [[SYKaoShiJuanDetailsView alloc] initWithFrame:CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT)];
//
//            UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
//            [window addSubview:shidetailsView];
//
//            [UIView animateWithDuration:kAnimate animations:^{
//                shidetailsView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
//            } completion:^(BOOL finished) {
//                [shidetailsView requestDataImageArr:papeFilePath];
//            }];

            self->browser = [WMPhotoBrowser new];
            self->browser.dataSource = papeFilePath;
            self->browser.downLoadNeeded = YES;
            self->leftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 60, 60)];
            [self->leftBtn setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
            
            [self->leftBtn addTarget:self action:@selector(backToFirstVC:) forControlEvents:UIControlEventTouchUpInside];
            
            UIApplication *ap = [UIApplication sharedApplication];
            
            [ap.keyWindow addSubview:self->browser.view];
            [ap.keyWindow addSubview:self->leftBtn];
            
        } fail:^(NSError *error) {
            [MBProgressHUD mj_hideHUDForView:self];
            [XSInfoView showInfo:error.message onSubView:self];
            
//            NSMutableArray *dataArr = [NSMutableArray array];
//            [dataArr addObject:@"http://image.31waiyu.com/TestCheckImage/270_1_4040_0_1.png"];
//            [dataArr addObject:@"http://image.31waiyu.com/TestCheckImage/270_1_4040_0_1.png"];
            
            
            
        }];

    }
    
    
    
}
-(void)backToFirstVC:(UIButton *)sender{
    [browser.view removeFromSuperview];
    [leftBtn removeFromSuperview];
}
#pragma mark - SYTongJiNameViewDelegate

- (void)tongjiNameViewSelectBtnType:(SYTongJiNameViewType)type {
    __weak typeof(self) weakSelf = self;
    switch (type) {
        case SYTongJiNameViewTypeXueSheng://按照学生
            self.twoTopLabView.hidden = YES;
            self.topLabView.hidden = NO;
            self.tjType.hidden = NO;
            self.isTongJi = NO;
            self.isKewen = NO;//按照课文统计
            self.topKewenView.hidden = YES;
            if (self.dataListArr.count > 0) {
                 [self.dataListArr removeAllObjects];
            }
            [self.curTableView reloadData];
            [self requestAnZhaoXueShengTongJi];
            break;
        case SYTongJiNameViewTypeShiTi://按照试题
            self.twoTopLabView.hidden = NO;//头部信息
            self.topLabView.hidden = YES;//表头
            self.tjType.hidden = YES;//状态view
            self.isTongJi = YES;//统计格式
            self.isKewen = NO;//按照课文统计
            self.topKewenView.hidden = YES;
            
           
            [self.curTableView reloadData];
            [self requestAnZhaoShiTiTongJi];
            break;
        case SYTongJiNameViewTypeKeWen:
            self.twoTopLabView.hidden = YES;//头部信息
            self.topLabView.hidden = YES;//表头
            self.tjType.hidden = YES;//状态view
            self.isTongJi = YES;//统计格式
            self.isKewen = YES;
            self.topKewenView.hidden = NO;
            if (self.dataListArr.count > 0) {
                 [self.dataListArr removeAllObjects];
            }
            [self.curTableView reloadData];
            [self requestKewenTongJi];
            
            break;
        case SYTongJiNameViewTypeXingMing://选择姓名
        {
            _xueshengView = [[NSBundle mainBundle] loadNibNamed:@"SYKSXuanZeXueShengView" owner:nil options:nil].firstObject;
            [_xueshengView showKaoShiBanJi:_xueshengView dataArr:self.classesModel.dataStudentsArr];
            
            _xueshengView.block = ^(id obj, id objTwo) {
                weakSelf.xueshengIds = (NSString *)obj;
                weakSelf.tjNameView.xingmingLab.text = (NSString *)objTwo;
                [weakSelf requestContAPI];
            };
            
        }
            break;
        case SYTongJiNameViewTypeBanJi://选择班级
        {
            _banjiView = [[NSBundle mainBundle] loadNibNamed:@"SYKaoShiBanJiView" owner:nil options:nil].firstObject;
            
            [_banjiView showKaoShiBanJi:_banjiView dataArr:self.infoModel.dataClassesArr];
            _banjiView.block = ^(id obj, id objTwo) {//请求数据
                SYExamInfoClassesModel *classesModel = (SYExamInfoClassesModel *)objTwo;
                //请求数据
                weakSelf.banjiId = (NSString *)obj;
                weakSelf.tjNameView.banjiLab.text = classesModel.className;
                //赋值
                weakSelf.classesModel = classesModel;
                [weakSelf requestContAPI];
            };
        }
            break;
            
            
        default:
            break;
    }
}

#pragma mark - BanJiTopViewSelectedDelegate
//考试统计
- (void)leftButtonClick {
    self.isKaoShi = YES;
    if ([self.type isEqualToString:@"1"]) {
        self.tjNameView.kewenBtn.hidden = NO;
    } else {
        self.tjNameView.kewenBtn.hidden = YES;
    }
    
    [self requestContAPI];
    
}
//订正统计
- (void)rightButtonClick {
    
    if ([self.type integerValue] == 5) {
        return;
    }
    self.isKaoShi = NO;
    self.isKewen = NO;
    self.tjNameView.kewenBtn.hidden = YES;
    [self requestContAPI];
}
//请求数据
- (void)requestContAPI {
    if (self.isKewen) {
        [self requestKewenTongJi];
    } else if (self.isTongJi == YES) {//按照试题统计
        [self requestAnZhaoShiTiTongJi];
    } else {//按学生统计
        [self requestAnZhaoXueShengTongJi];
    }
        
    
}


//查看试卷
- (void)backBtnClick {
    SYKaoShiJuanDetailsView  *shidetailsView = [[SYKaoShiJuanDetailsView alloc] initWithFrame:CGRectMake(0, kScreen_HEIGHT, kScreen_WIDTH, kScreen_HEIGHT)];
    
    UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
    [window addSubview:shidetailsView];
    [UIView animateWithDuration:kAnimate animations:^{
        shidetailsView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
        [shidetailsView requestDataListTemplateId:self.templateId];
    }];
}

#pragma mark - request请求数据 获取到考试信息
- (void)requestDataListModel:(SYExamInfoModel *)indfoModel {
    self.infoModel = indfoModel;
    self.tongjiView.infoModel = indfoModel;
    SYExamInfoClassesModel *classesModel = indfoModel.dataClassesArr.firstObject;
    self.classesModel = classesModel;
//    classesModel.isSelect = YES;
    self.tjNameView.banjiLab.text = classesModel.className;
    self.tjNameView.xingmingLab.text = @"全部";
    
    self.templateId = indfoModel.templateId;//模板id
    self.banjiId = classesModel.classId;
    NSString *studentIds = nil;
    for (SYExamInfoStudentsModel *model in classesModel.dataStudentsArr) {
        model.isSelect = model.newisSelect;
        if (model.isSelect == YES) {
            if (studentIds.length == 0) {
                studentIds = model.studentId;
            } else {
                studentIds = [NSString stringWithFormat:@"%@,%@",studentIds,model.studentId];
            }
        }
    }
    self.xueshengIds = studentIds;
    
    [self requestContAPI];
}
//按照试题统计
- (void)requestAnZhaoShiTiTongJi {
    __weak typeof(self) weakSelf = self;
//    [self.dataListArr removeAllObjects];
    self.isTongJi = YES;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestStatisticsServiceByQuestionClassIds:self.banjiId studentIds:self.xueshengIds templateId:self.templateId kaoshiOrDingzheng:self.isKaoShi success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [XSInfoView showInfo:error.message onSubView:self];
        
    }];
    
}
//课文统计
- (void)requestKewenTongJi {
    __weak typeof(self)weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI tj_requestStatisticsServiceByKnowledgeScheduleId:self.scheduleId templateId:self.templateId classId:self.banjiId sort:@"0" success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        [weakSelf.curTableView reloadData];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
         [XSInfoView showInfo:error.message onSubView:self];
    }];
    
    
}


- (void)requestAnZhaoXueShengTongJi {
    __weak typeof(self) weakSelf = self;
//    [self.dataListArr removeAllObjects];
    self.isTongJi = NO;

    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestStatisticsServiceByStudentsClassIds:self.banjiId studentIds:self.xueshengIds templateId:self.templateId kaoshiOrDingzheng:self.isKaoShi andSort:sort success:^(NSMutableArray *dataListArr) {
        [MBProgressHUD mj_hideHUDForView:self];
        weakSelf.dataListArr = dataListArr;
        [weakSelf.curTableView reloadData];
        
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
        [XSInfoView showInfo:error.message onSubView:self];
    }];
    
    
}


#pragma mark- set
- (BanJiTopView *)banJiTopView {
    if (!_banJiTopView) {
        _banJiTopView = [[BanJiTopView alloc] init];
        _banJiTopView.delegate = self;
        _banJiTopView.timeButton.hidden = YES;
        [_banJiTopView.weiJieYeButton setTitle:@"考试统计" forState:UIControlStateNormal];
        [_banJiTopView.jieYeButton setTitle:@"订正统计" forState:UIControlStateNormal];
        _banJiTopView.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    }
    return _banJiTopView;
}

- (SYBackTopView *)backTopView {
    if (!_backTopView) {
        _backTopView = [[SYBackTopView alloc] initWithTitleName:@"" baseView:self];
    }
    return _backTopView;
}
- (UIButton *)chakanBtn {
    if (!_chakanBtn) {
        _chakanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_chakanBtn setTitle:@"查看试卷" forState:UIControlStateNormal];
        [_chakanBtn setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        //[_chakanBtn setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        _chakanBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_chakanBtn setTitleColor:SELECTEDCOLOR forState:0];
        [_chakanBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _chakanBtn;
}
- (SYTongJiTopView *)tongjiView {
    if (!_tongjiView) {
        _tongjiView = [[NSBundle mainBundle] loadNibNamed:@"SYTongJiTopView" owner:nil options:nil].firstObject;
    }
    return _tongjiView;
}

- (SYTongJiNameView *)tjNameView {
    if (!_tjNameView) {
        _tjNameView = [[NSBundle mainBundle] loadNibNamed:@"SYTongJiNameView" owner:nil options:nil].firstObject;
        _tjNameView.banjiBtn.layer.borderColor = [UIColor colorWithHexString:@"#999999"].CGColor;
        _tjNameView.banjiBtn.layer.borderWidth = 1;
        
        _tjNameView.nameBtn.layer.borderColor = [UIColor colorWithHexString:@"#999999"].CGColor;
        _tjNameView.nameBtn.layer.borderWidth = 1;
        
        _tjNameView.shitiBtn.layer.borderColor = [UIColor colorWithHexString:@"#20B097"].CGColor;
        _tjNameView.shitiBtn.layer.borderWidth = 1;
        _tjNameView.shitiBtn.layer.masksToBounds = YES;
        _tjNameView.shitiBtn.layer.cornerRadius = 5;
        
        _tjNameView.xueshengBtn.layer.borderColor = [UIColor colorWithHexString:@"#20B097"].CGColor;
        _tjNameView.xueshengBtn.layer.borderWidth = 1;
        _tjNameView.xueshengBtn.layer.masksToBounds = YES;
        _tjNameView.xueshengBtn.layer.cornerRadius = 5;
        
        _tjNameView.delegate = self;
        
        
    }
    return _tjNameView;
}
#pragma mark - 按学生统计
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        NSArray *arr ;
        if ([self.type integerValue] == 5) {
            arr = @[@"序号",@"姓名",@"总题/批改",@"对/错",@"正确率",@"得分率",@"排名"];
        }else{
            arr = @[@"序号",@"姓名",@"总题/批改",@"对/错",@"正确率",@"得分率",@"得分",@"排名"];
        }
        NSInteger num = arr.count;
        CGFloat with = (CGFloat)1.0/num;

        _topLabView = [[SYTopLabView alloc]initWithDataNameArr:arr width:with];
        _topLabView.hidden = YES;
        _topLabView.paiMingChangeButton.hidden = NO;
        _topLabView.backgroundColor = [UIColor redColor];
        [_topLabView.paiMingChangeButton addTarget:self action:@selector(paiMingClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topLabView;
}
- (void)paiMingClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSLog(@"排名点击了");
    if (sender.selected == YES) {
        sort = @"1";
        [self requestAnZhaoXueShengTongJi];
    }else{
        sort = @"0";
        [self requestAnZhaoXueShengTongJi];
    }
}
//- (SYTopLabView *)LianXtopLabView {
//    if (!_LianXtopLabView) {
//        _LianXtopLabView = [[SYTopLabView alloc]initWithDataNameArr:@[@"序号",@"姓名",@"总题/批改",@"对/错",@"正确率",@"得分率",@"排名"] width:1/7];
//    }
//    return _LianXtopLabView;
//}
#pragma mark - 按学生统计
- (SYTopLabView *)topKewenView {
    if (!_topKewenView) {
        NSArray *arr = @[@"序号",@"姓名",@"学号",@"总分",@"得分",@"得分率",@"总人数",@"排名"] ;
        
        NSInteger num = arr.count;
        CGFloat with = (CGFloat)1.0/num;
        _topKewenView.hidden = YES;
        _topKewenView = [[SYTopLabView alloc]initWithDataNameArr:arr width:with];
//        _topKewenView.hidden = YES;
        _topKewenView.paiMingChangeButton.hidden = YES;
        
    }
    return _topKewenView;
}
- (SYTongJiTypeView *)tjType {
    if (!_tjType) {
        _tjType = [[NSBundle mainBundle] loadNibNamed:@"SYTongJiTypeView" owner:nil options:nil].firstObject;
        _tjType.hidden = YES;
    }
    return _tjType;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
        [_curTableView registerCellWithNIbNames:@[@"SYTongJiKaoshiTableViewCell",@"SYTJStudentsTableViewCell",@"SYTongJiLianXiTableViewCell",@"SYLXStudentsTableViewCell",@"SYKeWenTableViewCell"]];
    }
    return _curTableView;
}

@end
























